package com.java.example.ui.dialog

import android.app.Activity
import android.app.AlertDialog
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.text.SpannableString
import android.text.Spanned
import android.text.TextPaint
import android.text.TextUtils
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.view.Gravity
import android.view.View
import android.view.WindowManager
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.java.example.R
import com.java.example.ui.activity.AgreementActivity
import com.java.example.ui.activity.PolicyActivity

class AgreeDialog private constructor(activity: Activity) : View.OnClickListener {
    private var listener: OnActionListener? = null
    private lateinit var mDialog: AlertDialog
    private var tvContent: TextView? = null
    private val mActivity: Activity = activity

    fun showDialog(context: Context) {
        mDialog = AlertDialog.Builder(context, R.style.normal_dialog).create()
        mDialog.setCancelable(false)
        mDialog.setCanceledOnTouchOutside(false)
        mDialog.show()
        val window = mDialog.getWindow()
        if (window != null) {
            window.setContentView(R.layout.dialog_agree)
            window.setGravity(Gravity.CENTER)
            window.setWindowAnimations(R.style.anim_panel_up_from_bottom)
            window.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
            //设置属性
            val params = window.attributes
            params.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND
            params.dimAmount = 0.5f
            window.attributes = params
            window.findViewById<View>(R.id.tv_sure).setOnClickListener(this)
            window.findViewById<View>(R.id.tv_cancel).setOnClickListener(this)
            tvContent = window.findViewById(R.id.tv_content)
            val agreeContent = context.getString(R.string.first_agree)
            if (!TextUtils.isEmpty(agreeContent)) {
                val index1 = agreeContent.indexOf("《用户协议》")
                val index2 = agreeContent.indexOf("《隐私政策》")
                val spannableString = SpannableString(agreeContent)
                spannableString.setSpan(MyClickText1(mActivity), index1, index1 + 6, Spanned.SPAN_INCLUSIVE_EXCLUSIVE)

                val spannableString1 = SpannableString(spannableString)
                spannableString1.setSpan(MyClickText2(mActivity), index2, index2 + 6, Spanned.SPAN_INCLUSIVE_EXCLUSIVE)
                tvContent!!.text = spannableString1
                tvContent!!.movementMethod = LinkMovementMethod.getInstance()
            }
        }
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.tv_cancel -> {
                mDialog.dismiss()
                mActivity.finish()
            }

            R.id.tv_sure -> {
                mDialog.dismiss()
                if (listener != null) {
                    listener!!.agree()
                }
            }
        }
    }

    interface OnActionListener {
        fun agree()
    }

    fun setOnActionListener(listener: OnActionListener?) {
        this.listener = listener
    }

    private inner class MyClickText1(mainActivity: Activity?) : ClickableSpan() {
        private val context: Context? = mainActivity

        override fun updateDrawState(ds: TextPaint) {
            super.updateDrawState(ds)
            //设置文本的颜色
            ds.color = ContextCompat.getColor(mActivity, R.color.mainColor)
            //超链接形式的下划线，false 表示不显示下划线，true表示显示下划线
            ds.isUnderlineText = false
        }

        override fun onClick(v: View) {
            val intent = Intent(mActivity, com.java.example.ui.activity.AgreementActivity::class.java)
            mActivity.startActivity(intent)
        }
    }

    private inner class MyClickText2(mainActivity: Activity?) : ClickableSpan() {
        private val context: Context? = mainActivity

        override fun updateDrawState(ds: TextPaint) {
            super.updateDrawState(ds)
            //设置文本的颜色
            ds.color = ContextCompat.getColor(mActivity, R.color.mainColor)
            //超链接形式的下划线，false 表示不显示下划线，true表示显示下划线
            ds.isUnderlineText = false
        }

        override fun onClick(v: View) {
            val intent = Intent(mActivity, PolicyActivity::class.java)
            mActivity.startActivity(intent)
        }
    }

    companion object {
        fun create(activity: Activity): AgreeDialog {
            return AgreeDialog(activity)
        }
    }
}
